package au.com.langdale.gwt.client.model;

/**
 * Base for all String properties.  Concrete properties must 
 * provide _get(), _set(), getLength(), and getName().
 * 
 * @author adv
 *
 */

public abstract class StringProperty extends Property {
	public StringProperty() {
		// serializable ctor
	}

	protected abstract String _get(Object model);
	protected abstract void _set(Object model, String value);

	public String get(Object model) {
		String value = _get(model);
		return value == null? "": value; 
	}
	public void set(Object model, String value) {
		_set(model, value);
	}
	public boolean isSet(Object model) {
		String value = _get(model);
		return value != null && new Boolean(value).booleanValue();
	}
	public void set(Object model, boolean value) {
		_set(model, Boolean.toString(value));
	}
	public String getStyleName() {
		return "string-field";
	}
	public String validate(Object model) {
		if(get(model).length() > getLength())
			return "The " + getName() + " must be " + getLength() + " characters long.";
		return null;
	}
}
